---
title: "Computational Documents: leaflet&crosstalk"
output:
flexdashboard::flex_dashboard:
orientation: rows
theme: lumen
source_code: embed
---
```{r setup, include=FALSE}
library(tidyverse)
library(geojsonio)
library(crosstalk)
library(leaflet)
library(d3scatter)
library(DT)
election = geojsonio::geojson_read("data/election.json", what = "sp")
election@data = election@data %>%
mutate(소득 = (소득*0.0001) %>% round)
colnames(election@data) = colnames(election@data) %>% str_replace(" ","")
pal = colorFactor(c("#0D7440", "#2A88C5", "#C10D0D"), c("국민의당", "더불어민주당","새누리당"))
# 툴팁에 담을 정보 생성
labs = lapply(seq(nrow(election@data)), function(i) {
paste0( '', election@data[i, "선거구"]," : ",election@data[i, "읍면동명"], '
',
"당선 : ", election@data[i, "당선"], '
',
"국민의당 : ", election@data[i, "국민의당"],'',
"더불어민주당 : ", election@data[i, "더불어민주당"], '
',
"새누리당 : ", election@data[i, "새누리당"], '
')
})
# 선거구 경계 생성
election_gu = maptools::unionSpatialPolygons(election, election$선거구)
shared_elec = SharedData$new(election)
shared_elec_df = SharedData$new(election@data, group=shared_elec$groupName())
shared_elec_dt = crosstalk::SharedData$new(election@data %>%
select(선거구,당선,읍면동명,소득,노령화지수, 새누리당, 더불어민주당, 국민의당), group=shared_elec$groupName())
```
Inputs {.sidebar data-width=400}
-----------------------------------------------------------------------
### 대한민국 제20대 총선
서울 개표결과
```{r}
filter_checkbox("당선", "당선", shared_elec_df, ~당선, inline=T)
filter_slider("소득", "소득", shared_elec_df, ~소득, width="75%")
filter_select("동", "동", shared_elec_df, ~읍면동명)
```
---
This [R Markdown](http://rmarkdown.rstudio.com/) document uses the experimental [crosstalk](https://github.com/rstudio/crosstalk) R package to enable interactivity between different widgets that display the same [Gapminder](https://www.gapminder.org/data/) data.
**Credits:**
The following packages were used:
[gapminder](https://cran.r-project.org/web/packages/gapminder/index.html),
[flexdashboard](https://cran.r-project.org/web/packages/flexdashboard/index.html),
[tidyverse](https://cran.r-project.org/web/packages/tidyverse/index.html),
[countrycode](https://cran.r-project.org/web/packages/countrycode/index.html),
[sf](https://cran.r-project.org/web/packages/sf/index.html),
[rnaturalearth](https://cran.r-project.org/web/packages/rnaturalearth/index.html)
[leaflet](https://cran.r-project.org/web/packages/leaflet/index.html), [d3scatter](https://github.com/jcheng5/d3scatter), [DT](https://cran.r-project.org/web/packages/DT/index.html).
Column {data-height=450}
-------------------------------------
### DataTable
```{r table}
DT::datatable(shared_elec_dt, width="100%")
```
Column {data-height=450}
-------------------------------------
### Map
```{r map}
leaflet(shared_elec) %>%
addProviderTiles("CartoDB.Positron") %>%
setView(126.982, 37.5502, zoom=10) %>%
addPolygons(
fillColor = ~pal(당선),
weight = 1,
opacity = 1,
color = "white",
dashArray=3,
fillOpacity = 0.5,
label = lapply(labs, htmltools::HTML),
highlightOptions=highlightOptions(bringToFront = F)) %>%
addLegend(pal = pal, values = ~당선, opacity = 0.7, title = NULL, position = "bottomright") %>%
addPolylines(
data=election_gu,
weight = 1.5,
opacity = 1,
color = "black")
```
### Plot
```{r plot}
d3scatter(shared_elec_df, ~소득, ~기초수급자인원수, ~당선, width="100%", height=250)
```